package com.huashi.dealer.modular.controller.store.goods;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huashi.dealer.commonEntity.Goods;
import com.huashi.dealer.core.page.PageFactory;
import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.category.param.CategoryListVo;
import com.huashi.dealer.modular.business.category.service.CategoryService;
import com.huashi.dealer.modular.business.goods.bo.GoodsAddBo;
import com.huashi.dealer.modular.business.goods.param.StoreGoodsListParam;
import com.huashi.dealer.modular.business.goods.param.StoreGoodsStatusParam;
import com.huashi.dealer.modular.business.goods.param.StoreGoodsSortParam;
import com.huashi.dealer.modular.business.goods.service.StoreGoodsService;
import com.huashi.dealer.modular.business.goods.vo.StoreGoodsListVo;
import com.huashi.dealer.modular.business.goods.vo.StoreGoodsDetailVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Store端商品控制器
 * 对应PHP项目的/store/goods/index界面
 *
 * @author 谭陈强
 * @since 2025-08-07
 */
@Slf4j
@Tag(name = "Store端商品控制器")
@RestController("storeGoodsController")
    @RequestMapping("/store/goods")
@Validated
public class GoodsController {

    @Resource
    private StoreGoodsService storeGoodsService;
    @Autowired
    private CategoryService categoryService;


    @GetMapping("/getCategoryIndex")
    @Operation(summary = "分类页面")
    public CommonResult<CategoryListVo> getCategoryIndex() {
        try {
            CategoryListVo responseParam = categoryService.getCategoryIndex();
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取分类页面失败：" + e.getMessage());
        }
    }
    /**
     * 新增商品
     */
    @Operation(summary = "新增商品")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody GoodsAddBo bo) {
        storeGoodsService.add(bo);
        return CommonResult.ok("添加成功");
    }
    @Operation(summary = "修改商品")
    @PutMapping("/update")
    public CommonResult<String> update(@RequestBody GoodsAddBo bo) {
        try {
            storeGoodsService.update(bo);
        }catch (Exception e){
            return CommonResult.error("修改商品：" + e.getMessage());
        }
        return CommonResult.ok("修改成功");
    }
    /**
     * 修改商品信息
     */
    @Operation(summary = "修改商品信息")
    @PostMapping("/updateGoods")
    public CommonResult<String> updateInfo(@RequestBody Goods good) {
        try {
            storeGoodsService.updateInfo(good);
        }catch (Exception e){
            return CommonResult.error("修改商品：" + e.getMessage());
        }
        return CommonResult.ok("修改成功");
    }

//    /**
//     * 商品分页
//     */
//    @GetMapping("/page")
//    @Operation(summary = "商品分页")
//    public CommonResult<Page<StoreGoodsListVo>> getGoodsList(StoreGoodsListParam param) {
//        Page<Goods> page= PageFactory.defaultPage();
//        try {
//            Page<StoreGoodsListVo> goodsList = storeGoodsService.getGoodsList(param, page);
//            return CommonResult.data(goodsList);
//        } catch (Exception e) {
//            log.error("获取商品列表失败", e);
//            return CommonResult.error("获取商品列表失败：" + e.getMessage());
//        }
//    }

    /**
     * 商品列表(出售中)
     */
    @GetMapping("/index")
    @Operation(summary = "商品列表")
    public CommonResult<Page<StoreGoodsListVo>> getGoodsList(StoreGoodsListParam param) {
            Page<Goods> page= PageFactory.defaultPage();
        try {
            Page<StoreGoodsListVo> goodsList = storeGoodsService.getGoodsList(param, page);
            return CommonResult.data(goodsList);
        } catch (Exception e) {
            log.error("获取商品列表失败", e);
            return CommonResult.error("获取商品列表失败：" + e.getMessage());
        }
    }

    /**
     * 商品详情
     */
    @GetMapping("/details/{goodsId}")
    @Operation(summary = "商品详情")
    public CommonResult<StoreGoodsDetailVo> getGoodsDetail(@PathVariable Long goodsId) {
        try {
            StoreGoodsDetailVo result = storeGoodsService.getGoodsDetail(goodsId);
            return CommonResult.data(result);
        } catch (Exception e) {
            log.error("获取商品详情失败", e);
            return CommonResult.error("获取商品详情失败：" + e.getMessage());
        }
    }

    /**
     * 修改商品状态（上架/下架）
     */
    @PostMapping("/state")
    @Operation(summary = "修改商品状态")
    public CommonResult<String> updateGoodsStatus(@RequestBody StoreGoodsStatusParam param) {
        try {
            storeGoodsService.updateGoodsStatus(param.getGoodsId(), param.getState());
            return CommonResult.ok("操作成功");
        } catch (Exception e) {
            log.error("修改商品状态失败", e);
            return CommonResult.error("操作失败：" + e.getMessage());
        }
    }

    /**
     * 修改商品新品状态
     */
    @PostMapping("/stateNew")
    @Operation(summary = "修改商品新品状态")
    public CommonResult<String> updateGoodsNewStatus(@RequestBody StoreGoodsStatusParam param) {
        try {
            storeGoodsService.updateGoodsNewStatus(param.getGoodsId(), param.getState());
            return CommonResult.ok("操作成功");
        } catch (Exception e) {
            log.error("修改商品新品状态失败", e);
            return CommonResult.error("操作失败：" + e.getMessage());
        }
    }

    /**
     * 修改商品爆款状态
     */
    @PostMapping("/stateHot")
    @Operation(summary = "修改商品爆款状态")
    public CommonResult<String> updateGoodsHotStatus(@RequestBody StoreGoodsStatusParam param) {
        try {
            storeGoodsService.updateGoodsHotStatus(param.getGoodsId(), param.getState());
            return CommonResult.ok("操作成功");
        } catch (Exception e) {
            log.error("修改商品爆款状态失败", e);
            return CommonResult.error("操作失败：" + e.getMessage());
        }
    }

    /**
     * 修改商品排序
     */
    @PostMapping("/sort")
    @Operation(summary = "修改商品排序")
    public CommonResult<String> updateGoodsSort(@RequestBody StoreGoodsSortParam param) {
        try {
            storeGoodsService.updateGoodsSort(param.getGoodsId(), param.getGoodsSort());
            return CommonResult.ok("操作成功");
        } catch (Exception e) {
            log.error("修改商品排序失败", e);
            return CommonResult.error("操作失败：" + e.getMessage());
        }
    }

    /**
     * 删除商品
     */
    @GetMapping("/delete/{goodsId}")
    @Operation(summary = "删除商品")
    public CommonResult<String> deleteGoods(@PathVariable Long goodsId) {
        try {
            storeGoodsService.deleteGoods(goodsId);
            return CommonResult.ok("删除成功");
        } catch (Exception e) {
            log.error("删除商品失败", e);
            return CommonResult.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 批量上架
     */
    @PostMapping("/allUpStatus")
    @Operation(summary = "批量上架")
    public CommonResult<String> batchUpStatus(@RequestBody List<Long> goodsIds) {
        try {
            storeGoodsService.batchUpStatus(goodsIds);
            return CommonResult.ok("批量上架成功");
        } catch (Exception e) {
            log.error("批量上架失败", e);
            return CommonResult.error("批量上架失败：" + e.getMessage());
        }
    }

    /**
     * 批量下架
     */
    @PostMapping("/allDownStatus")
    @Operation(summary = "批量下架")
    public CommonResult<String> batchDownStatus(@RequestBody List<Long> goodsIds) {
        try {
            storeGoodsService.batchDownStatus(goodsIds);
            return CommonResult.ok("批量下架成功");
        } catch (Exception e) {
            log.error("批量下架失败", e);
            return CommonResult.error("批量下架失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除
     */
    @PostMapping("/allDelete")
    @Operation(summary = "批量删除")
    public CommonResult<String> batchDelete(@RequestBody List<Long> goodsIds) {
        try {
            storeGoodsService.batchDelete(goodsIds);
            return CommonResult.ok("批量删除成功");
        } catch (Exception e) {
            log.error("批量删除失败", e);
            return CommonResult.error("批量删除失败：" + e.getMessage());
        }
    }
}
